﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LogManager.Base;
using LogManager.Windows.Data;
using LogManager.Windows;
using LogManager.Base.Data;
using LogManager.WebConfig;
using System.Configuration;

namespace LogManager.Configuration.Data
{
    public class WebConfigLogRepository : ILogRepository
    {
        private IQueryable<ILog> _logs;

        public WebConfigLogRepository(IServerRepository serverRepository,ILogBuilder builder)
        {
            _logs = (ConfigurationManager.GetSection("logConfiguration") as LogConfigurationSection).LogCollection.Cast<LogConfiguration>().Select(lc => builder.Build(serverRepository.Get(lc.ServerName), lc.LogName, lc.LogType)).AsQueryable();
        }

        #region ILogRepository Members

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        public IQueryable<ILog> Query()
        {
            return _logs;
        }

        public IEnumerable<ILog> All()
        {
            return _logs;
        }

        public ILog Get(string logId)
        {
            return _logs.Where(l => l.LogName == logId).SingleOrDefault();
        }

        #endregion

    }
}